package com.system.controller;

import com.system.entity.SysUser;
import com.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@CrossOrigin
@RestController
public class SysUserController {

    @Resource
    private ISysUserService userService;

    @GetMapping("/")
    public String preLog(){
        return "sysLogin";
    }

    @PostMapping("/sysLogin")
    @ApiOperation(value="登录")
    public String login(@RequestBody SysUser user){
        System.out.println(user.getUname());
        System.out.println(user.getUpass());


        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(user.getUname(), user.getUpass());
        System.out.println(token);


        try {
            subject.login(token);
            return "main";
        }catch (AuthenticationException e){
            e.printStackTrace();
            return "login";
        }

//        if (userService.login(username, password) != null){
//            return "main";
//        }else{
//            return "login";
//        }
    }
}
